<DOCTYPE html>
  <html lang="en">

  <head charset="UTF-8">
    <title></title>
  </head>

  <body>
    <script>
      function Stack() {
        this.age = 15;
        this.array = [];

        Stack.prototype.getAge = () => {
          return this.age;
        }

        Stack.prototype.setAge = num => {
          this.age = num;
        }

        //1.栈结构添加方法
        Stack.prototype.push = element => {
          this.array.push(element);
        }

        //2.删除方法并展示删除元素
        Stack.prototype.pop = () => {
          return this.array.pop();
        }

        //3.查看栈底的第一个元素
        Stack.prototype.peek = () => {
          return this.array[0];
        }

        //4.查看数组中是否有元素,无为true,有为false
        Stack.prototype.isEmpty = () => {
          return this.array.length == 0
        }

        //5.返回栈长度
        Stack.prototype.size = () => {
          return this.array.length
        }

        //6.返回栈元素转换为字符串
        Stack.prototype.toString = () => {
          let result = "";
          for (let i = 0; i < this.array.length; i++) {
            result += this.array[i] + ' ';
          }
          return result
        }
      }

      //十进制转二进制方法
      function tenToTwo(num) {
        let array = new Stack();
        while (num > 0) {
          array.push(num % 2);
          num = Math.floor(num / 2);
        }
        let result = ''
        while (array.size() != 0) {
          result += array.pop()
        }
        return result
      }
    </script>
  </body>

  </html>